package com.vancouver.knows.ui.fragment.citydiscount;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.location.Location;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.browser.ListItemBrowser;
import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.DiscountInfo;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.ui.view.FlowLayout;
import com.vancouver.knows.util.AppUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MapDiscountResultListView extends ListItemBrowser<MainApplication> {

	private BusinessModule bModule;
	private List<DiscountInfo> datas;

	public MapDiscountResultListView(Context context) {
		super(context);
		init();
	}

	public MapDiscountResultListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public void flush(){
		notifyDataSetChanged();
	}

	public void setDatas(List<DiscountInfo> datas) {
		this.datas = datas;
	}

	private void init() {
		bModule = imContext.getBusinessModule();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		return false;
	}

	@Override
	protected BaseAdapter createAdapter() {
		return new CityDiscountsAdapter();
	}

	@Override
	protected void setListViewParameters(ListView listView) {
		super.setListViewParameters(listView);
		listView.setDivider(new ColorDrawable(getResources().getColor(R.color.line_color)));
		listView.setDividerHeight(1);
	}

	@Override
	protected void handleLoadNewItems(ATaskMark taskMark) {
	}

	public class CityDiscountsAdapter extends BaseAdapter {

		private LayoutInflater inflater;

		public CityDiscountsAdapter() {
			inflater = LayoutInflater.from(getContext());
		}

		@Override
		public int getCount() {
			return datas==null?0:datas.size();
		}

		@Override
		public Object getItem(int position) {
			return datas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = createItemView(parent);
			}

			DiscountInfo discountInfo = (DiscountInfo) getItem(position);
			if (discountInfo != null) {
				initItemView(convertView, discountInfo);
			}

			return convertView;
		}

		private View createItemView(ViewGroup parent) {
			View view = inflater.inflate(R.layout.business_item_layout, parent, false);
			ViewHolder viewHolder = new ViewHolder(view);
			view.setTag(viewHolder);

			return view;
		}

		private void initItemView(View convertView, final DiscountInfo discountInfo) {
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getContext(), CityDiscountDetailActivity.class);
					intent.putExtra("postId", discountInfo.getId());
					getContext().startActivity(intent);
				}
			});

			ViewHolder viewHolder = (ViewHolder) convertView.getTag();
			viewHolder.title.setText(discountInfo.getName());
			if (!discountInfo.getImages().isEmpty()) {
				viewHolder.attachmenImagesContainer.setVisibility(VISIBLE);
				viewHolder.singleImage.setVisibility(GONE);
				if (discountInfo.getImages().size() > 2) {
					Picasso.with(imContext).load(discountInfo.getImages().get(0)).placeholder(R.drawable.no_image_placeholder).centerCrop()
							.fit()
							.into(viewHolder.imageOne);
					Picasso.with(imContext).load(discountInfo.getImages().get(1)).placeholder(R.drawable.no_image_placeholder).centerCrop()
							.fit()
							.into(viewHolder.imageTwo);
					Picasso.with(imContext).load(discountInfo.getImages().get(2)).placeholder(R.drawable.no_image_placeholder).centerCrop()
							.fit()
							.into(viewHolder.imageThree);
				} else {
					viewHolder.attachmenImagesContainer.setVisibility(GONE);
					viewHolder.singleImage.setVisibility(VISIBLE);
					Picasso.with(imContext).load(discountInfo.getThumbnail()).placeholder(R.drawable.no_image_placeholder).centerCrop()
							.fit()
							.into(viewHolder.singleImage);
				}
			} else {
				if (!StringUtil.isEmptyString(discountInfo.getThumbnail())) {
					viewHolder.attachmenImagesContainer.setVisibility(GONE);
					viewHolder.singleImage.setVisibility(VISIBLE);
					Picasso.with(imContext).load(discountInfo.getThumbnail()).placeholder(R.drawable.no_image_placeholder).centerCrop()
							.fit()
							.into(viewHolder.singleImage);
				} else {
					viewHolder.singleImage.setImageResource(R.drawable.no_image_placeholder);
				}
			}

			viewHolder.ratingBar.setRating((float) discountInfo.getRating());
			viewHolder.ratingBar.setNumStars(5);
			viewHolder.commentCount.setText(String.valueOf(discountInfo.getCommentCount()));
			float[] distance = new float[1];
			Location.distanceBetween(discountInfo.getBusinessLat(), discountInfo.getBusinessLng(), imContext.latitude, imContext.longitude, distance);
			if (distance[0] == 0.0f) {
				viewHolder.distance.setVisibility(GONE);
			} else {
				viewHolder.distance.setVisibility(VISIBLE);
				viewHolder.distance.setText(AppUtils.createDistanceStrShort((long) distance[0], getResources()
						.getColor(R.color.orange)));
			}
			viewHolder.category.setText(discountInfo.getCategory());

			if (discountInfo.getPromotionTypes() != null && discountInfo.getPromotionTypes().length > 0) {
				viewHolder.promotionTypesContainer.setVisibility(VISIBLE);
				for (String promotionType : discountInfo.getPromotionTypes()) {
					switch (promotionType) {
						case "置顶":
							viewHolder.badgeOne.setVisibility(VISIBLE);
							viewHolder.badgeOne.setText(promotionType);
							viewHolder.badgeOne.setBackgroundResource(R.drawable.promotion_type_green_bg);
							break;

						case "外卖":
							viewHolder.badgeTwo.setVisibility(VISIBLE);
							viewHolder.badgeTwo.setText(promotionType);
							viewHolder.badgeTwo.setBackgroundResource(R.drawable.promotion_type_blue_bg);
							break;

						case "福利":
							viewHolder.badgeThree.setVisibility(VISIBLE);
							viewHolder.badgeThree.setText(promotionType);
							viewHolder.badgeThree.setBackgroundResource(R.drawable.promotion_type_orange_bg);
							viewHolder.promotionDescription.setText(discountInfo.getPromotionDescription());
							break;
					}
				}
			} else {
				viewHolder.promotionTypesContainer.setVisibility(GONE);
			}
		}

		public class ViewHolder {

			@Bind(R.id.single_image)
			ImageView singleImage;
			@Bind(R.id.title)
			TextView title;
			@Bind(R.id.attachment_imgs_container)
			LinearLayout attachmenImagesContainer;
			@Bind(R.id.post_attachment_img_one)
			ImageView imageOne;
			@Bind(R.id.post_attachment_img_two)
			ImageView imageTwo;
			@Bind(R.id.post_attachment_img_three)
			ImageView imageThree;
			@Bind(R.id.ratingBar)
			RatingBar ratingBar;
			@Bind(R.id.commentCount)
			TextView commentCount;
			@Bind(R.id.distance)
			TextView distance;
			@Bind(R.id.category)
			TextView category;
			@Bind(R.id.promotionTypes)
			FlowLayout promotionTypesContainer;
			@Bind(R.id.first_badge)
			TextView badgeOne;
			@Bind(R.id.second_badge)
			TextView badgeTwo;
			@Bind(R.id.third_badge)
			TextView badgeThree;
			@Bind(R.id.promotionDescription)
			TextView promotionDescription;

			public ViewHolder(View view) {
				ButterKnife.bind(this, view);
			}

		}

	}

}
